<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\AddArticleRequest;
use App\Models\Article;
use Illuminate\Http\Request;

class ArticleController extends BaseController
{
    /**
     * 文章列表显示
     */
    public function index(Request $request)
    {
        // 验证ajax请求
        if ($request->header('X-Requested-With') == 'XMLHttpRequest') {
            // 起始位置
            //  ----- 起始位置默认设置为0 --------
            $start = $request->get('start', 0);
            // 获取每页的记录数----
            //  ----- 防止有人恶意修改记录数为巨大的数,设置最大值100, 默认值10 ---------
            $length = min(100, $request->get('length', 10));
            // 获取数据
            // 搜索条件获取
            $datamin = $request->get('datamin');
            $datamax = $request->get('datamax');
            $title   = $request->get('title');

            ['column' => $column, 'dir' => $dir] = $request->get('order')[0];//解构赋值
            $columns = $request->get('columns');
            $name    = $columns[$column]['data'];

            $query = Article::where('id', '>', 0);
            if (!empty($datamin) && !empty($datamax)) {
                $datamin = date('Y-m-d H-i-s', strtotime($datamin . ' 00:00:00'));
                $datamax = date('Y-m-d H-i-s', strtotime($datamax . ' 00:00:00'));
                $query   = $query->whereBetween('created_at', [$datamin, $datamax]);
            }
            if (!empty($title)) {
                $query = $query->where('title', 'like', "%{$title}%");
            }
            $count  = $query->count();
            $data   = $query->offset($start)->orderBy($name, $dir)->limit($length)->get();
            $result = [
                'draw'            => $request->get('draw'),
                'recordsTotal'    => $count,
                'recordsFiltered' => $count,
                'data'            => $data
            ];
            return json_encode($result);
        }
        return view('admin.article.index');

    }

    /**
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function create()
    {
        return view('admin.article.create');
    }

    /**
     * 新建文章保存
     * @param AddArticleRequest $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function store(AddArticleRequest $request)
    {
        $res = $request->except('_token', 'file');
        // dd($res);
        Article::create($res);
        return redirect(route('admin.article.index'));
    }

    /**
     * Display the specified resource.
     *
     * @param \App\Models\Article $article
     * @return \Illuminate\Http\Response
     */
    public function show(Article $article)
    {
        //
    }

    /**
     * @param Article $article
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function edit(Article $article)
    {
        return view('admin.article.edit1', compact('article'));
    }

    /**
     * @param AddArticleRequest $request
     * @param Article $article
     * @return array
     */
    public function update(AddArticleRequest $request, Article $article)
    {
        $data = $request->except('action', 'file', '_method', '_token');
        // dd($data);
        if ($article->update($data)) {
            return redirect(route('admin.article.index'));
            return ['code' => 200, 'msg' => '修改成功', 'route' => '/admin/article'];
        } else {
            return redirect(route('admin.article.index'));
            return ['code' => 400, 'msg' => '修改失败', 'route' => ''];
        }
    }

    /**
     * @param Article $article
     * @return array
     */
    public function destroy(Article $article)
    {
        $article->delete();
        return ['code' => 201, 'msg' => '删除成功'];
    }

    public function upimg(Request $request)
    {
        $pic = config('uploadpic.pic');
        if ($request->hasFile('file')) {
            $info = $request->file('file')->store('', 'article');
            $pic  = '/uploads/article/images/' . $info;
        }
        return ['code' => 200, 'src' => $pic];
    }
}
